package com.teamwhole.core.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.teamwhole.common.utils.StringUtils;

public class PageServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private String prefix = PREFIX;
	private String suffix = SUFFIX;
	private String defaultPath = DEFAULT_PATH;

	@Override
	public void init() throws ServletException {
		prefix = getInitParameter("prefix");
		suffix = getInitParameter("suffix");
		defaultPath = getInitParameter("defaultPath");
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = getPath(request);
		// TODO 判断当前操作对象是否拥有访问该页面的权限
		// Subject subject = UserUtils.getSubject();
		// System.out.println(subject);
		request.getRequestDispatcher(prefix + path + suffix).forward(request, response);
	}

	private String getPath(HttpServletRequest request) {
		String path = request.getPathInfo();
		if (StringUtils.isBlank(path) || "/".equals(path)) {
			path = defaultPath;
		}
		return path;
	}

	public static final String PREFIX = "/WEB-INF/views/modules";
	public static final String SUFFIX = ".jsp";
	public static final String DEFAULT_PATH = "/index";

}
